package com.kehutong.wxapi.service.impl;

import com.kehutong.wxapi.request.ContactBatchSearchRequest;
import com.kehutong.wxapi.request.ContactSearchRequest;
import com.kehutong.wxapi.response.ContactBatchSearchResponse;
import com.kehutong.wxapi.response.ContactSearchResponse;
import com.kehutong.wxapi.service.ContactService;
import com.kehutong.wxapi.util.KhtClient;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSON;
import org.coraframework.json.JSONObject;

/**
 * 通讯录搜索服务
 * @author: liukunlong
 * @date: 2021-03-08 19:13
 */
public class ContactServiceImpl implements ContactService {
    @Inject
    private KhtClient khtClient;
    /**
     * 通讯录单个搜索
     * 请求地址：https://qyapi.weixin.qq.com/cgi-bin/service/contact/search?provider_access_token=ACCESS_TOKEN
     * @param contactSearchRequest 通讯录单个搜索请求对象
     * @return 通讯录单个搜索响应对象
     * @throws Exception 异常
     */
    public ContactSearchResponse contactSearch(ContactSearchRequest contactSearchRequest) throws Exception{
        JSONObject jsonObject = khtClient.get("/cgi-bin/service/contact/search", http -> {
            http.addParam("access_token", contactSearchRequest.getProviderAccessToken());
            contactSearchRequest.setProviderAccessToken(null);
            http.setBody(JSON.toJSONString(contactSearchRequest));
        });
        return JSON.parseObject(jsonObject.toJSONString(), ContactSearchResponse.class);
    }


    /**
     * 通讯录批量搜索
     * 请求地址：https://qyapi.weixin.qq.com/cgi-bin/service/contact/batchsearch?provider_access_token=ACCESS_TOKEN
     * @param contactBatchSearchRequest 通讯录批量搜索请求对象
     * @return 通讯录批量搜索搜索响应对象
     * @throws Exception 异常
     */
    public ContactBatchSearchResponse contactBatchSearch(ContactBatchSearchRequest contactBatchSearchRequest) throws Exception{
        JSONObject jsonObject = khtClient.get("/cgi-bin/service/contact/batchsearch", http -> {
            http.addParam("access_token", contactBatchSearchRequest.getProviderAccessToken());
            contactBatchSearchRequest.setProviderAccessToken(null);
            http.setBody(JSON.toJSONString(contactBatchSearchRequest));
        });
        return JSON.parseObject(jsonObject.toJSONString(), ContactBatchSearchResponse.class);
    }
}
